# Maintainer: Alexey Pavlov <Alexpux@gmail.com>

_realname=bullet
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}" "${MINGW_PACKAGE_PREFIX}-${_realname}-debug")
pkgver=2.87
pkgrel=2
pkgdesc="A 3D Collision Detection and Rigid Body Dynamics Library for games and  animation (mingw-w64)"
arch=('any')
url="https://www.bulletphysics.com/Bullet/"
license=('custom:zlib')
options=('!strip' 'staticlibs')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-freeglut"
             "${MINGW_PACKAGE_PREFIX}-openvr"
             "${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-python3-numpy")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/bulletphysics/bullet3/archive/${pkgver}.tar.gz"
        mingw-fixes.patch
        mingw-strlen-missing-include.patch
        mingw-link-rearrange.patch
        mingw-allow-shared-builds.patch
        fix-runtime-install.patch
        install-openvr-only-msvc.patch)
sha256sums=('438c151c48840fe3f902ec260d9496f8beb26dba4b17769a4a53212903935f95'
            'df3ece9dd216c3ab406f1d79849b96ee3b5eef727623f00d23d8c9d3aed06f09'
            '5ff773be3814aaaf7776da5787a702188d7009fec2ca2434d6cf139f2b5a5177'
            '6e23969747bd73e29ea6d6cdb7abebca5e61782ce42d163f0a7c65649cd16203'
            '2e9cc177007635b4126c230790c989e33371263abecba0f3cae94dd052abb556'
            '942fb30ef816428f009fbde7ef955d6f23fb5d69ccb267d043f3efff8f43290c'
            'bc089e5ffac4b0d902a0b2fea9db8645ab385ee62ba82a3d739b00c272ed45fb')

prepare () {
  cd ${srcdir}/bullet3-${pkgver}
  patch -p1 -i ${srcdir}/mingw-fixes.patch
  patch -p1 -i ${srcdir}/mingw-strlen-missing-include.patch
  patch -p1 -i ${srcdir}/mingw-link-rearrange.patch
  patch -p1 -i ${srcdir}/mingw-allow-shared-builds.patch
  patch -p1 -i ${srcdir}/fix-runtime-install.patch
  patch -p1 -i ${srcdir}/install-openvr-only-msvc.patch
}

build() {
  plain "No step"
}

build_release() {
  depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
           "${MINGW_PACKAGE_PREFIX}-freeglut"
           "${MINGW_PACKAGE_PREFIX}-openvr")
  install=${_realname}-${CARCH}.install

  [[ -d build-release ]] && rm -rf build-release
  mkdir -p build-release && pushd build-release
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_EXTRAS=ON \
    -DINSTALL_LIBS=ON \
    -DUSE_GLUT=ON \
    -DCMAKE_CXX_FLAGS_RELEASE="-fpermissive" \
    -DINSTALL_EXTRA_LIBS=ON \
    -DBUILD_PYBULLET_NUMPY=ON \
    -DCMAKE_BUILD_TYPE=Release \
    ../bullet3-${pkgver}

  make
  make DESTDIR=${pkgdir} install

  mkdir -p ${pkgdir}${MINGW_PREFIX}/share/bullet
  for f in $(find examples -type f -iname *.dll -o -iname *.exe); do
    cp ${f} ${pkgdir}${MINGW_PREFIX}/share/bullet/
  done
  cp -rf ${srcdir}/bullet3-${pkgver}/data/* ${pkgdir}${MINGW_PREFIX}/share/bullet/
  for f in gtest gwen OpenGLWindow; do
    cp lib/lib${f}.dll ${pkgdir}${MINGW_PREFIX}/share/bullet/
  done
}

build_debug() {
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}=${pkgver}-${pkgrel}")

  [[ -d build-debug ]] && rm -rf build-debug
  mkdir -p build-debug && pushd build-debug
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_EXTRAS=ON \
    -DINSTALL_LIBS=ON \
    -DUSE_GLUT=ON \
    -DCMAKE_CXX_FLAGS_RELEASE="-fpermissive" \
    -DINSTALL_EXTRA_LIBS=ON \
    -DBUILD_PYBULLET_NUMPY=ON \
    -DCMAKE_BUILD_TYPE=Debug \
    -DCMAKE_DEBUG_POSTFIX="_d" \
    ../bullet3-${pkgver}

  make
  make DESTDIR=${pkgdir} install

  rm -rf ${pkgdir}${MINGW_PREFIX}/include
  rm -rf ${pkgdir}${MINGW_PREFIX}/lib/cmake
  rm -rf ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig

}

package_mingw-w64-i686-bullet() {
  build_release
}

package_mingw-w64-i686-bullet-debug() {
  build_debug
}

package_mingw-w64-x86_64-bullet() {
  build_release
}

package_mingw-w64-x86_64-bullet-debug() {
  build_debug
}
